package com.youko.backstage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youko.backstage.bean.Commodity;
import com.youko.backstage.dto.commodity.CommodityDto;
import com.youko.backstage.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommodityService {
    @Autowired
    private CommodityMapper commodityMapper;

    /**
     * 添加商品
     * @param commodityDto
     */
    public void saveCommodity(CommodityDto commodityDto){
        commodityMapper.insertCommodity(commodityDto.getName(), commodityDto.getPrice(),commodityDto.getNameOfMer(), commodityDto.getSpecs(), commodityDto.getTag());
    }

    /**
     * 修改商品
     * @param commodityDto
     */
    public void updateCommodity(CommodityDto commodityDto){
        commodityMapper.updateCommodity(commodityDto.getName(),commodityDto.getPrice(), commodityDto.getSpecs(),commodityDto.getTag());
    }

    /**
     * 删除商品
     * @param id
     */
    public void deleteCommodity(int id){
        commodityMapper.deleteCommodity(id);
    }


    /**
     * 添加图片文件服务器端路径
     * @param id
     * @param path
     */
    public void updateCommodityPhotoPath(int id,String path){
        commodityMapper.updateCommodityPhotoPath(id,path);
    }


    /**
     * 分页查询商品
     * @param pageNum
     * @param pageSize
     * @param nameOfMer
     * @return
     */
    public List<Commodity> selectAll(int pageNum,int pageSize,String nameOfMer){
        PageHelper.startPage(pageNum,pageSize);
        List<Commodity> commodities=commodityMapper.selectAll(nameOfMer);
        PageInfo<Commodity> commodityPageInfo=new PageInfo<>(commodities);
        return commodityPageInfo.getList();
    }



}
